//
//  OAMyBriefcaseHtmlViewController.m
//  ERP
//
//  Created by QA on 16/6/27.
//  Copyright © 2016年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "OAMyBriefcaseHtmlViewController.h"
#import "AESCrypt.h"
#import "TYMActivityIndicatorView.h"

@interface OAMyBriefcaseHtmlViewController ()<UIWebViewDelegate,TYMActivityIndicatorViewDelegate>

@property (nonatomic,strong) NSMutableArray *applyArray;
@property (nonatomic, strong) TYMActivityIndicatorView *activityIndicatorView;
@property (nonatomic, strong) NSMutableURLRequest *request;

@end

@implementation OAMyBriefcaseHtmlViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"公文明细"];
    }
    return self;
}

- (id)initWithNibUrl:(NSString *)urlString
{
    NSURL *url = [NSURL URLWithString:urlString];
    self.request = [[NSMutableURLRequest alloc] initWithURL:url];
    return  [super init];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webView.delegate = self;
    
    //创建进度条
    if (!self.activityIndicatorView) {
        CGSize size = CGSizeMake(SuperWidth, SuperHeight - 64);
        CGFloat offsetX = size.width/2 - 107.0f/2;
        CGFloat offsetY = size.height/2 - 107.0f/2;
        
        self.activityIndicatorView = [[TYMActivityIndicatorView alloc] initWithActivityIndicatorStyle:TYMActivityIndicatorViewStyleLarge];
        _activityIndicatorView.hidesWhenStopped = YES;
        _activityIndicatorView.frame = CGRectMake(offsetX, offsetY, 107.0f, 107.0f);
        _activityIndicatorView.delegate = self;
        _activityIndicatorView.userInteractionEnabled = YES;
        [self.view addSubview:_activityIndicatorView];
    }
    
    self.webView.scalesPageToFit = YES;
    _webView.scrollView.bouncesZoom = NO;
    [self.webView loadRequest:_request];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies])
        [storage deleteCookie:cookie];
    
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

#pragma mark -
#pragma mark TYMActivityIndicatorViewDelegate

- (void)refreshSelector:(id)button
{
    self.activityIndicatorView.hidden = NO;
    [self.activityIndicatorView startAnimating];
    [self.webView loadRequest:_request];
}

#pragma mark -
#pragma mark UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    _webView.userInteractionEnabled = NO;
    if (!self.activityIndicatorView.isAnimating) {
        self.activityIndicatorView.hidden = NO;
        [self.activityIndicatorView startAnimating];
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    _webView.userInteractionEnabled = YES;
    if (self.activityIndicatorView.isAnimating) {
        self.activityIndicatorView.hidden = YES;
        [self.activityIndicatorView stopAnimating];
    }
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"===didFailLoadWithError===%@",error);
    _webView.userInteractionEnabled = NO;
    [self.activityIndicatorView showErrorView];
}

#pragma mark
#pragma mark - UIAlertView

- (void)setAlertViewMessage:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}

@end
